package com.tencent.sr.iris.basic.mp.restapi.controller;

import com.tencent.sr.iris.basic.mp.interfaces.request.GetUploadUrlRequest;
import com.tencent.sr.iris.basic.mp.interfaces.request.GetVisitUrlRequest;
import com.tencent.sr.iris.basic.mp.interfaces.request.UserMaterialRequest;
import com.tencent.sr.iris.basic.mp.interfaces.response.GetUploadUrlResponse;
import com.tencent.sr.iris.basic.mp.interfaces.response.GetVisitUrlResponse;
import com.tencent.sr.iris.basic.mp.service.CosService;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.material.interfaces.client.MaterialCosClient;
import com.tencent.sr.rmall.material.interfaces.request.GetCosTmpCredentialRequest;
import com.tencent.sr.rmall.material.interfaces.response.GetCosTmpCredentialResponse;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Copyright (C) 2021 Limited, a Tencent company. All rights reserved.
 *
 * @author cookyan@tencent.com
 * @date 2021/12/19 10:57 下午
 */
@RestController
@Slf4j
@Api("cos相关接口")
@RequestMapping("/iris/basic/mp/v1/cos")
public class CosController {


    @Resource
    private AppUser appUser;

    @Resource
    private CosService cosService;

    @Resource
    private MaterialCosClient materialCosClient;

    @PostMapping("/getUploadUrl")
    @ApiOperation("获取上传地址")
    public HttpResult<GetUploadUrlResponse> getUploadUrl(@RequestBody @Validated GetUploadUrlRequest request) {
        log.info("getUploadUrl request {}", JsonUtils.toJsonString(request));
        request.setSaasId(String.valueOf(appUser.getSaasId()));
        request.setUid(appUser.getUid());

        return HttpResult.succ(cosService.getUploadPicUrl(request));
    }

    @PostMapping("/getVisitUrl")
    @ApiOperation("获取访问地址")
    public HttpResult<GetVisitUrlResponse> getVisitUrl(@RequestBody @Validated GetVisitUrlRequest request) {
        log.info("getVisitUrl request: {}", JsonUtils.toJsonString(request));
        request.setSaasId(String.valueOf(appUser.getSaasId()));
        request.setUid(appUser.getUid());

        return HttpResult.succ(cosService.getVisitUrl(request));
    }

    @ApiOperation(value = "获取临时上传凭证")
    @PostMapping(value = "/getTmpCredential")
    public HttpResult<GetCosTmpCredentialResponse> getTmpCredential(@RequestBody
            UserMaterialRequest request) {
        GetCosTmpCredentialRequest getCosTmpCredentialRequest = new GetCosTmpCredentialRequest();
        getCosTmpCredentialRequest.setDirType(Objects.isNull(request.getDirType()) ? 1 : request.getDirType());
        getCosTmpCredentialRequest.setSubAccount(appUser.getSaasId().toString());
        getCosTmpCredentialRequest.setUserId(appUser.getUid());
        getCosTmpCredentialRequest.setBizName(request.getBizName());
        getCosTmpCredentialRequest.setMaterialType(request.getMaterialType());
        getCosTmpCredentialRequest.setBuckType(request.getBuckType());
        HttpResult<GetCosTmpCredentialResponse> httpResult = materialCosClient
                .getTmpCredential(getCosTmpCredentialRequest);
        return httpResult;
    }

}
